import java.io.*;
import java.net.*;



public class Http
{
       
        
        
    private Socket httpSocket = null;  
    private BufferedReader inputStream = null; 
    private BufferedWriter outputStream = null; 
    
              
       
      /**
       * Makes an HTTP GET request for the URL specified and returns the servers response.
       */              
      public String HTTP_GET(String urlString) throws MalformedURLException, UnknownHostException, IOException{
            return HTTP_GET(urlString, "http://www.google.co.uk");      
      }
      
      
      
      /**
       * Makes an HTTP GET request for the URL specified returns the servers response. This method closes all the streams it uses.
       */       
      public String HTTP_GET(String urlString, String referer) throws MalformedURLException, UnknownHostException, IOException{
            HTTP_GET_InputStream(urlString, referer);
            String result = getResponse(inputStream);
            closeAll();
            return result;
      }
      
      
      
      /**
       * Makes an HTTP GET request fot the URL spceified, and returns the input stream. The stream can then be used to capture the servers response
       * directly.
       *
       * If explicitly using this method, you must explicitly use closeAll() when finished with the stream (not the streams close() method). 
       */      
      public BufferedReader HTTP_GET_InputStream(String urlString, String referer) throws MalformedURLException, UnknownHostException, IOException{
            return HTTP_InputStream(urlString, referer, HTTP_GET_Headers(new URL(urlString), referer));
      }            
      
      
      
      /*
       * Provides standard HTTP GET headers based on input values.
       */
      private String HTTP_GET_Headers(URL url_in, String referer_in){
            String result = "";
            result += "GET " + url_in.getFile() +  " HTTP/1.1\n";
          result += "Host: " + url_in.getHost() + "\n";
          result += "Accept: text/html\n";
            result += "Referer: " + referer_in + "\n";
          result += "Accept-Language: en-gb\n";
            result += "User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows 98; Win 9x 4.90)\n";
          result += "Connection: close\n";
        result += "\n";
            return result;
      }
      
      
      
      /**
       * Makes an HTTP request to the URL spceified, with the headers specified as output - returns the input stream. 
       * The stream can then be used to capture the servers response directly.
       *
       * If explicitly using this method, you must explicitly use closeAll() when finished with the stream (not the streams close() method). 
       */      
      public BufferedReader HTTP_InputStream(String urlString, String referer, String headers) throws MalformedURLException, UnknownHostException, IOException{

            /***************************************************************************************************************************
             */
          httpSocket = null;  
        inputStream = null; 
        outputStream = null; 
        
        // the string buffer which stores the incoming HTML
        StringBuffer stringBuffer = new StringBuffer(); 
        
        try{
              httpSocket = new Socket((new URL(urlString)).getHost(), 80);
            outputStream = new BufferedWriter(new OutputStreamWriter(httpSocket.getOutputStream()));
            inputStream = new BufferedReader(new InputStreamReader(httpSocket.getInputStream()));
        } 
            /***************************************************************************************************************************
             */
        // catch any error conditions that have occured when constructing the above objects
        catch (MalformedURLException e){
              throw new MalformedURLException("MalformedURLException:" + urlString);
            }
        catch (UnknownHostException e) {
              throw new UnknownHostException("UnknownHostException:" + urlString);
        } 
        catch (IOException e) {
            throw new IOException("IOException:\n" + e);
        }
        /***************************************************************************************************************************
             */
          try{      
            outputStream.write(headers); 
            //outputStream.write("\r\n", 0, 2);
                  outputStream.flush();
          } 
        catch (IOException e){
            throw new IOException("IOException:\n" + e);
        }
            return inputStream; 
      } // end method
      
      
      
      /**
       * This method reads the data form the input stream, and returns it as a string.
       */
      public String getResponse(BufferedReader inputStream_in) throws IOException{
            
            String responseLine;
            StringBuffer stringBuffer = new StringBuffer();
                  
            while (true){
                try{
                      responseLine = inputStream_in.readLine(); // read a line from the server output, convert all to lower case - easier to work with one or the other but not both
                }
              catch (IOException e){
                  throw new IOException("IOException in server interaction:\n" + e);
              }
                if (responseLine == null)
                break;
            stringBuffer.append(responseLine); // add the read string to the buffer
        }
            return stringBuffer.toString();
      }
      
      
            
      public void closeAll(){
            
            try{inputStream.close();}
            catch(Exception e){}
            finally{inputStream = null;}
            
            try{outputStream.close();}
            catch(Exception e){}
            finally{outputStream = null;}
            
            try{httpSocket.close();}
            catch(Exception e){}
            finally{httpSocket = null;}
            
      }
      
      
      
} // end class
/*
--------------------------------------
ACCEPTED SOLUTION
 	
06.29.2006 at 05:20AM PDT, ID: 17008802
hoomanv:
spoof user agent

con.setRequestProperty("User-Agent", "Mozilla/5.0");
*/